<template>
	<view>
		<form @submit="SubmitUserName">
			<view class="userName">
				<input v-model="name" name="userName" placeholder="账号名/邮箱/手机号" type="text">
				<view class="hr-class"></view>
			</view>
			<view class="userPassword">
				<input v-model="pass" name="pass" placeholder="请输入密码" type="text">
				<view class="hr-class"></view>
			</view>
			<button form-type="submit">登录</button>
			<button @click="zhuce">注册</button>
		</form>
	</view>
</template>

<script>
	import {
		loginName
	} from '../appjs/longin/login';


	export default {
		data() {
			return {
				name: '',
				pass: ''
			}
		},
		methods: {
			zhuce(){
				uni.redirectTo({
					url:"/pages/register/register"
				})
			},
			SubmitUserName: function(e) {
				var formdata = JSON.stringify(e.detail.value)
				let thiw = this;
				let flg = false;
				console.log('form发生了submit事件，携带数据为：' + formdata)
			
				loginName(thiw.name, thiw.pass).then(data => {
					console.log("用户信息1212", data.data);
					console.log("用户信息1", data);
					data.data.forEach((item, index) => {
						if (thiw.name == item.userName && thiw.pass == item.userPassword) {
							this.$user.username = item.userName;
							this.$user.password = item.userPassword;
							this.$user.uid = item.userId;
							console.log(this.$user.uid);
							uni.switchTab({
								url: "/pages/index/index",
							});
							flg = true;
						}
					});
					if (!flg) {
						uni.redirectTo({
							url: "/pages/longin/longin",
							success() {
								uni.showToast({
									title: "账号或密码错误",
									icon: 'error',
									duration: 1000
								});
							}
						})
					}
				})
			},
		}
	}
</script>

<style>
	.userName {
		margin-left: 80px;
		margin-top: 40px;
	}

	.userPassword {
		margin-left: 80px;
		margin-top: 40px;
	}

	.hr-class {
		border-bottom: 1px solid #eaeaea;
		/* 分割线颜色 */
		margin: 20px;
		/* 上下边距 */
		margin-left: 0px;
		width: 300;
	}
</style>